package com.caifu.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caifu.excel.ExcelDatas;
import com.caifu.mapper.BmActivityManagerMapper;
import com.caifu.mapper.CouponsRecordMapper;
import com.caifu.mapper.SysOrganizationMapper;
import com.caifu.pojo.BmActivityManager;
import com.caifu.pojo.BmCouponsRecord;
import com.caifu.pojo.ExcelData;
import com.caifu.pojo.SysUser;
import com.caifu.util.Util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author tianwenwen
 * ClassName: BmCouponsRecordService.java
 * version: 1.0.0
 * createTime: 2020年10月16日 10:59:38
 **/
@Service
public class BmCouponsRecordService {

    @Resource
    private CouponsRecordMapper couponsRecordMapper;

    @Resource
    private SysOrganizationMapper sysOrganizationMapper;

    @Resource
    private BmActivityManagerMapper bmActivityManagerMapper;

    /**
     * description: 根据老人id获取老人优惠券记录 type： 1.理发 2.缝纫
     * param id type
     * return: com.baomidou.mybatisplus.core.metadata.IPage<com.caifu.pojo.BmCouponsRecord>
     * author: Tian
     * time: 2020/10/28 10:11
     */
    public IPage<BmCouponsRecord> getRecordById(String id, String type, String startTime, String endTime, String currentPage, String pageSize) {
        QueryWrapper<BmCouponsRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bm_userinfo_id", id).eq("type", type).orderByDesc("created_time");
        if (Util.isNotNull(startTime) && Util.isNull(endTime)) {
            queryWrapper.gt("created_time", startTime);
        } else if (Util.isNull(startTime) && Util.isNotNull(endTime)) {
            queryWrapper.lt("created_time", endTime);
        } else if (Util.isNotNull(startTime) && Util.isNotNull(endTime)) {
            queryWrapper.between("created_time", startTime, endTime);
        }
        Page<BmCouponsRecord> page = new Page<>(Long.parseLong(currentPage), Long.parseLong(pageSize));
        IPage<BmCouponsRecord> bmCouponsRecordIPage = couponsRecordMapper.selectPage(page, queryWrapper);
        List<BmCouponsRecord> records = bmCouponsRecordIPage.getRecords();
        String hair = "1";
        String cut = "2";
        for (BmCouponsRecord record : records) {
            if (hair.equals(record.getHairCut())) {
                record.setNumber("+" + record.getNumber());
            } else if (cut.equals(record.getHairCut())) {
                record.setNumber("-" + record.getNumber());
            }
        }
        return bmCouponsRecordIPage;
    }

    public List<Map<String, Object>> getExcelData(String type, String flag, SysUser user) throws Exception {
        List<Map<String, Object>> maps = new ArrayList<>();
        //根据type获取所有活动室
        List<BmActivityManager> bmActivityManagers = bmActivityManagerMapper
                .selectList(new QueryWrapper<BmActivityManager>()
                        .like("type", type)
                        .eq("status", 1));
        Map<String, Object> map = new HashMap<>(10);
        map.put("type", type);
        //1.本月 2.本年
        String years = "";
        if ("1".equals(flag)) {
            years = "and DATE_FORMAT(bcr.created_time, '%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')";
        } else if ("2".equals(flag)) {
            years = "and YEAR(bcr.created_time)=YEAR(NOW())";
        }
        map.put("flag", years);
        for (BmActivityManager bmActivityManager : bmActivityManagers) {
            Map<String, Object> datas = new HashMap<>(10);
            map.put("activityId", bmActivityManager.getId());
            List<ExcelData> excelDataList = couponsRecordMapper.getExcelData2(map);
            if (excelDataList.size() > 0) {
                for (int i = 0; i < excelDataList.size(); i++) {
                    excelDataList.get(i).setNums(i + 1);
                }
            }
            datas.put("name", bmActivityManager.getName());
            datas.put("data", excelDataList);
            maps.add(datas);
        }
        return formatData(maps);
    }

    /**
     * 获取当前管理员组织架构
     */
    private HashSet<Long> getSysUserOrgId(SysUser user) throws Exception {
        //查询当前管理员类型为3得节点
        Map<String, Object> param = new HashMap<>();
        param.put("userNo", String.valueOf(user.getUserNo()));
        param.put("type", 3);
        List<Map<String, Object>> orgList = sysOrganizationMapper.selectOrgIdsByUserNoAndType(param);
        HashSet<Long> longs = new HashSet<>();
        for (Map<String, Object> map : orgList) {
            String[] codes = map.get("orgPathCode").toString().split(",");
            for (String code : codes) {
                longs.add(Long.parseLong(code));
            }
        }
        return longs;
    }

    private List<Map<String, Object>> formatData(List<Map<String, Object>> datas) {
        List<Map<String, Object>> mapList = datas;
        for (Map<String, Object> data : mapList) {
            List<ExcelData> excelDataList = (List<ExcelData>) data.get("data");
            for (ExcelData excelData : excelDataList) {
                //性别
                if ("1".equals(excelData.getUserSex())) {
                    excelData.setUserSex("男");
                } else {
                    excelData.setUserSex("女");
                }
                if ("1".equals(excelData.getType())) {
                    excelData.setType("理发");
                } else {
                    excelData.setType("缝纫");
                }
            }
        }
        return mapList;
    }
}
